<?php
class Pesanan extends CI_Model {
	function __construct() {
		parent::__construct();
	}
	
	function get($where = NULL, $type = 'none'){
		if($where != NULL){
			$this->db->where($where);
		}
		$this->db->from('pesanan');
		if($type == 'koki'){
			$this->db->order_by('status', 'ASC');
		}
		if($type == 'kasir'){
			$this->db->order_by('status', 'DESC');
		}
		return $this->db->get();
	}
	
	function add($data){
		$this->db->insert('pesanan',$data);
		return $this->db->insert_id();
	}
	
	function edit($id, $data){
		$this->db->where('id_pesanan',$id);
		return $this->db->update('pesanan',$data);
	}
	
	function delete($id){
		$this->db->where('id_pesanan',$id);
		return $this->db->delete('pesanan');
	}
	
	//detail pesanan
	function get_detail_pesanan($id_pesanan){
		$this->db->select('*');
		$this->db->from('detail_pesanan as dp');
		$this->db->join('paket_makanan as p', 'dp.id_paket = p.id_paket');
		$this->db->where('dp.id_pesanan', $id_pesanan);
		return $this->db->get()->result_array();
	}
	
	function add_detail_pesanan($data){
		$this->db->insert('detail_pesanan',$data);
		return $this->db->insert_id();
	}
	
	//laporan keuangan
	function get_income($from = NULL, $to = NULL){
		$this->db->select('SUM(jumlah_bayar) as pendapatan');
		$this->db->from('pesanan');
		if($from != NULL && $to != NULL){
			$this->db->where('waktu_pesan >=', $from);
			$this->db->where('waktu_pesan <=', $to);
		}else{
			$this->db->where('DATE(waktu_pesan) = CURDATE()');
		}
		$query = $this->db->get()->row_array();
		return $query['pendapatan'];
	}
	
}